//
//  ProfileItem.m
//  ProfilesManager
//
//  Created by Vescky on 14/10/27.
//  Copyright (c) 2014年 Vescky. All rights reserved.
//

#import "ProfileItem.h"

@implementation ProfileItem 

@synthesize appName,enviroment,createDate,expireDate,bundleId,teamName,uuid;


+ (ProfileItem*)readProfileContent:(NSString*)profilePath {
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:profilePath]) {
        NSLog(@"Warming:File not exist: %s\n",[profilePath UTF8String]);
        return nil;
    }
    
    NSString *fileString = [[NSString alloc] initWithContentsOfFile:profilePath encoding:NSASCIIStringEncoding error:nil];
    NSScanner *scanner = [[NSScanner alloc] initWithString:fileString];
    
    ProfileItem *pitem = [[ProfileItem alloc] init];
    
    if ([scanner scanUpToString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" intoString:NULL]) {
        NSString *plistString;
        if ([scanner scanUpToString:@"</plist>" intoString:&plistString]) {
            NSDictionary *plist = [[plistString stringByAppendingString:@"</plist>"] propertyList];
            pitem.appName = [plist objectForKey:@"Name"];
            pitem.enviroment = [[plist objectForKey:@"Entitlements"] objectForKey:@"aps-environment"];
            pitem.createDate = [plist objectForKey:@"CreationDate"];
            pitem.expireDate = [plist objectForKey:@"ExpirationDate"];
            pitem.bundleId = [[plist objectForKey:@"Entitlements"] objectForKey:@"application-identifier"];
            pitem.teamName = [plist objectForKey:@"TeamName"];
            pitem.uuid = [plist objectForKey:@"UUID"];
        }
    }
    
    return pitem;
}


@end
